package cn.ibizlab.odoo.core.odoo_stock.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Istock_rules_report;
import cn.ibizlab.odoo.core.odoo_stock.domain.Stock_rules_report;
import cn.ibizlab.odoo.core.odoo_stock.filter.Stock_rules_reportSearchContext;
import cn.ibizlab.odoo.core.odoo_stock.service.IStock_rules_reportService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[库存规则报告] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Stock_rules_reportServiceImpl implements IStock_rules_reportService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Istock_rules_reportClientService stock_rules_reportClientService;

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //stock_rules_reportClientService.removeBatch(stock_rules_reports) ;

    }

    @Override
    public boolean create(Stock_rules_report stock_rules_report) {
        boolean bOk = false ;
        Istock_rules_report clientModel = this.convert2Model(stock_rules_report,null);
        stock_rules_reportClientService.create(clientModel) ;
        this.convert2Domain(clientModel,stock_rules_report);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void createBatch(List<Stock_rules_report> stock_rules_reports, int batchSize) {

                        //stock_rules_reportClientService.createBatch(stock_rules_reports,batchSize) ;

    }

    @Override
    public Stock_rules_report get(Integer id) {
        Istock_rules_report clientModel = stock_rules_reportClientService.createModel();
        clientModel.setId(id);
        stock_rules_reportClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Stock_rules_report get(Stock_rules_report stock_rules_report) {
        Stock_rules_report retstock_rules_report = this.get(stock_rules_report.getId()) ;
    	BeanCopier copier=BeanCopier.create(retstock_rules_report.getClass(), stock_rules_report.getClass(), false);
        copier.copy(retstock_rules_report, stock_rules_report, null);
        return stock_rules_report ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Istock_rules_report clientModel = stock_rules_reportClientService.createModel();
        clientModel.setId(id);
        stock_rules_reportClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean update(Stock_rules_report stock_rules_report) {
        boolean bOk = false ;
        Istock_rules_report clientModel = this.convert2Model(stock_rules_report,null);
        stock_rules_reportClientService.update(clientModel) ;
        this.convert2Domain(clientModel,stock_rules_report) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void updateBatch(List<Stock_rules_report> stock_rules_reports, int batchSize) {

        //stock_rules_reportClientService.updateBatch(stock_rules_reports,batchSize) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Stock_rules_report> searchDefault(Stock_rules_reportSearchContext context) {
        List<Stock_rules_report> list = new ArrayList<Stock_rules_report>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_rules_report> clientModelList = stock_rules_reportClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_rules_report clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_rules_report>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据产品查询
     */
    @Override
    public Page<Stock_rules_report> selectByOdoo_product(Integer id){
        Stock_rules_reportSearchContext context = new Stock_rules_reportSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_rules_report> list = new ArrayList<Stock_rules_report>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_rules_report> clientModelList = stock_rules_reportClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_rules_report clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_rules_report>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据产品模板查询
     */
    @Override
    public Page<Stock_rules_report> selectByOdoo_product_tmpl(Integer id){
        Stock_rules_reportSearchContext context = new Stock_rules_reportSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_rules_report> list = new ArrayList<Stock_rules_report>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_rules_report> clientModelList = stock_rules_reportClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_rules_report clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_rules_report>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Stock_rules_report> selectByOdoo_create(Integer id){
        Stock_rules_reportSearchContext context = new Stock_rules_reportSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_rules_report> list = new ArrayList<Stock_rules_report>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_rules_report> clientModelList = stock_rules_reportClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_rules_report clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_rules_report>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Stock_rules_report> selectByOdoo_write(Integer id){
        Stock_rules_reportSearchContext context = new Stock_rules_reportSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_rules_report> list = new ArrayList<Stock_rules_report>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_rules_report> clientModelList = stock_rules_reportClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_rules_report clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_rules_report>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Istock_rules_report convert2Model(Stock_rules_report domain , Istock_rules_report model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = stock_rules_reportClientService.createModel() ;
        }

         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getWarehouse_idsDirtyFlag())
            model.setWarehouse_ids(domain.getWarehouse_ids());
         if(domain.getProduct_has_variantsDirtyFlag())
            model.setProduct_has_variants(domain.getProduct_has_variants());
         if(domain.getSo_route_idsDirtyFlag())
            model.setSo_route_ids(domain.getSo_route_ids());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getProduct_id_textDirtyFlag())
            model.setProduct_id_text(domain.getProduct_id_text());
         if(domain.getProduct_tmpl_id_textDirtyFlag())
            model.setProduct_tmpl_id_text(domain.getProduct_tmpl_id_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getProduct_idDirtyFlag())
            model.setProduct_id(domain.getProduct_id());
         if(domain.getProduct_tmpl_idDirtyFlag())
            model.setProduct_tmpl_id(domain.getProduct_tmpl_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
        return model ;
    }

    public Stock_rules_report convert2Domain( Istock_rules_report model ,Stock_rules_report domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Stock_rules_report();
        }

        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getWarehouse_idsDirtyFlag())
            domain.setWarehouse_ids(model.getWarehouse_ids());
        if(model.getProduct_has_variantsDirtyFlag())
            domain.setProduct_has_variants(model.getProduct_has_variants());
        if(model.getSo_route_idsDirtyFlag())
            domain.setSo_route_ids(model.getSo_route_ids());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getProduct_id_textDirtyFlag())
            domain.setProduct_id_text(model.getProduct_id_text());
        if(model.getProduct_tmpl_id_textDirtyFlag())
            domain.setProduct_tmpl_id_text(model.getProduct_tmpl_id_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getProduct_idDirtyFlag())
            domain.setProduct_id(model.getProduct_id());
        if(model.getProduct_tmpl_idDirtyFlag())
            domain.setProduct_tmpl_id(model.getProduct_tmpl_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        return domain ;
    }


}
